#include "Problem13.hpp"

Problem13::Problem13(Index dimension) : UnconstrainedProblem(dimension) {
	assert(dimension >= 2);
}

bool Problem13::get_nlp_info(Index& n, Index& nnz_h_lag) {
	n = dimension;
	nnz_h_lag = 0;

	return true;
}

bool Problem13::get_starting_point(Index n, Number* x) {
	for (Index i = 0; i < dimension; ++i) {
		x[i] = 1;
	}

	return true;
}

bool Problem13::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) {
	Number sum = 0;
	for (int i = 0; i <= n-2; i++) {
		sum += (x[i]*x[i]+x[n-1]*x[n-1])*(x[i]*x[i]+x[n-1]*x[n-1])-4*x[i]+3;
	}
	obj_value = sum;
	return true;
}

bool Problem13::eval_grad_f(Index n, const Number* x, bool new_x,
		Number* grad_f) {

	grad_f[n-1]=0;
	for (int i = 0; i <= n - 2; i++) {
				grad_f[i] = 4*x[i]*(x[n-1]*x[n-1] + x[i]*x[i]) - 4;
				grad_f[n-1]+=4*x[n-1]*(x[n-1]*x[n-1] + x[i]*x[i]);
			}



	return true;
}

void Problem13::newX(const Number* x) {
}

